Skip to content

Commit

Permalink
Observer Updates (#82)
Browse files Browse the repository at this point in the history
* Updated Observer to v1.6.1

* Removed unnecessary undefineds and updated observer
  • Loading branch information
kpal81xd authored Jan 15, 2025
1 parent 8119e5d commit 9f24a9e
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 53 deletions.
13 changes: 8 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@babel/eslint-parser": "^7.23.3",
"@babel/preset-env": "^7.23.8",
"@playcanvas/eslint-config": "2.0.8",
"@playcanvas/observer": "^1.5.1",
"@playcanvas/observer": "^1.6.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
Expand Down Expand Up @@ -67,6 +67,7 @@
"docs": "typedoc",
"lint": "eslint src rollup.config.mjs",
"lint:fix": "eslint src rollup.config.mjs --fix",
"type:check": "tsc --noEmit",
"publint": "publint"
},
"engines": {
Expand Down
13 changes: 10 additions & 3 deletions src/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class Asset extends Events {
this._observer.apiAsset = this;
this._observer.addEmitter(this);

(this._observer as any).latestFn = () => {
this._observer.latestFn = () => {
const latest = api.assets.get(this.get('id'));
return latest && latest._observer;
};
Expand Down Expand Up @@ -509,7 +509,7 @@ class Asset extends Events {
* @returns {boolean} Whether the value was inserted
*/
insert(path: any, value: any, index: any) {
return (this._observer as any).insert(path, value, index, undefined, undefined);
return this._observer.insert(path, value, index);
}

/**
Expand All @@ -520,7 +520,7 @@ class Asset extends Events {
* @returns {boolean} Whether the value was removed
*/
removeValue(path: any, value: any) {
return (this._observer as any).removeValue(path, value, undefined, undefined);
return this._observer.removeValue(path, value);
}

/**
Expand Down Expand Up @@ -643,6 +643,13 @@ class Asset extends Events {
replace(this, asset, options);
}

/**
* The observer object for this asset.
*/
get observer() {
return this._observer;
}

/**
* Gets observer history for this asset.
*
Expand Down
34 changes: 17 additions & 17 deletions src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ class Assets extends Events {

this._uniqueIdToItemId = {};

(this._assets as any) = new ObserverList({
this._assets = new ObserverList({
index: 'id',
sorted: ((a: any, b: any) => {
sorted: (a: any, b: any) => {
const f = +(b._data.type === 'folder') - +(a._data.type === 'folder');
if (f !== 0) {
return f;
Expand All @@ -186,7 +186,7 @@ class Assets extends Events {
return -1;
}
return 0;
}) as any
}
});

this._parseScriptCallback = null;
Expand Down Expand Up @@ -262,7 +262,7 @@ class Assets extends Events {
* @returns {Asset} The asset
*/
get(id: number) {
const a = (this._assets as any).get(id);
const a = this._assets.get(id);
return a ? a.apiAsset : null;
}

Expand All @@ -283,7 +283,7 @@ class Assets extends Events {
* @returns {Asset[]} The assets
*/
list() {
return (this._assets as any).array().map((a: { apiAsset: any; }) => a.apiAsset);
return this._assets.array().map((a: { apiAsset: any; }) => a.apiAsset);
}

/**
Expand Down Expand Up @@ -330,18 +330,18 @@ class Assets extends Events {
add(asset: Asset) {
asset._initializeHistory();

const pos = (this._assets as any).add((asset as any)._observer);
const pos = this._assets.add(asset.observer);
if (pos === null) return;

const id = asset.get('id');
this._uniqueIdToItemId[asset.get('uniqueId')] = id;

(asset as any)._observer.on('name:set', (name: string, oldName: string) => {
asset.observer.on('name:set', (name: string, oldName: string) => {
name = (name || '').toLowerCase();
oldName = (oldName || '').toLowerCase();

const ind = (this._assets as any).data.indexOf((asset as any)._observer);
let pos = (this._assets as any).positionNextClosest((asset as any)._observer, (a: any, b: any) => {
const ind = this._assets.data.indexOf(asset.observer);
let pos = this._assets.positionNextClosest(asset.observer, (a: any, b: any) => {
const f = +(b._data.type === 'folder') - +(a._data.type === 'folder');

if (f !== 0) {
Expand All @@ -357,7 +357,7 @@ class Assets extends Events {

});

if (pos === -1 && (ind + 1) === (this._assets as any).data.length) {
if (pos === -1 && (ind + 1) === this._assets.data.length) {
return;
}

Expand All @@ -369,7 +369,7 @@ class Assets extends Events {
pos--;
}

(this._assets as any).move((asset as any)._observer, pos);
this._assets.move(asset.observer, pos);
this.emit('move', asset, pos);
});

Expand All @@ -384,12 +384,12 @@ class Assets extends Events {
* @param {Asset} asset - The asset
*/
remove(asset: Asset) {
if (!(this._assets as any).has((asset as any)._observer)) return;
if (!this._assets.has(asset.observer)) return;

(this._assets as any).remove((asset as any)._observer);
this._assets.remove(asset.observer);

delete this._uniqueIdToItemId[asset.get('uniqueId')];
(asset as any)._observer.destroy();
asset.observer.destroy();

if (api.realtime) {
api.realtime.assets.unload(asset.get('uniqueId'));
Expand All @@ -408,7 +408,7 @@ class Assets extends Events {
const assets = this.list();
if (!assets.length) return;

(this._assets as any).clear();
this._assets.clear();

let i = assets.length;
while (i--) {
Expand All @@ -425,7 +425,7 @@ class Assets extends Events {
* @returns {Asset[]} The assets
*/
filter(fn: Function) {
return (this._assets as any).data
return this._assets.data
.filter((observer: { apiAsset: any; }) => fn(observer.apiAsset))
.map((observer: { apiAsset: any; }) => observer.apiAsset);
}
Expand All @@ -437,7 +437,7 @@ class Assets extends Events {
* @returns {Asset} The asset
*/
findOne(fn: Function) {
const result = (this._assets as any).data.find((observer: { apiAsset: any; }) => fn(observer.apiAsset));
const result = this._assets.data.find((observer: { apiAsset: any; }) => fn(observer.apiAsset));
return result ? result.apiAsset : null;
}

Expand Down
18 changes: 9 additions & 9 deletions src/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Entities extends Events {
* ```
*/
get(id: string) {
const e = (this._entities as any).get(id);
const e = this._entities.get(id);
return e ? e.apiEntity : null;
}

Expand All @@ -136,7 +136,7 @@ class Entities extends Events {
* ```
*/
list() {
return (this._entities as any).array().map((e: { apiEntity: any; }) => e.apiEntity);
return this._entities.array().map((e: { apiEntity: any; }) => e.apiEntity);
}

/**
Expand All @@ -153,7 +153,7 @@ class Entities extends Events {
return;
}

(this._entities as any).add((entity as any)._observer);
this._entities.add(entity.observer);
if (!entity.get('parent')) {
if (this._root) {
console.error(`More than one root entities in Scene. Current root is Entity "${this._root.get('name')}" [${this._root.get('resource_id')}] but Entity "${entity.get('name')}" [${id}] also has a null parent`);
Expand Down Expand Up @@ -212,8 +212,8 @@ class Entities extends Events {

// remove from observer list
try {
(this._entities as any).remove((entity as any)._observer);
(entity as any)._observer.destroy();
this._entities.remove(entity.observer);
entity.observer.destroy();
} catch (err) {
console.error(err);
}
Expand Down Expand Up @@ -244,8 +244,8 @@ class Entities extends Events {
});
}

(this._entities as any).remove((entity as any)._observer);
(entity as any)._observer.destroy();
this._entities.remove(entity.observer);
entity.observer.destroy();

if (this._root === entity) {
this._root = null;
Expand All @@ -262,7 +262,7 @@ class Entities extends Events {
clear() {
this._root = null;
const entities = this.list();
(this._entities as any).clear();
this._entities.clear();

if (api.selection) {
if (api.selection.items[0] instanceof Entity) {
Expand All @@ -274,7 +274,7 @@ class Entities extends Events {
}

entities.forEach((entity: any) => {
(entity as any)._observer.destroy();
entity.observer.destroy();
this.emit('remove', entity);
});

Expand Down
2 changes: 1 addition & 1 deletion src/entities/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async function deleteEntities(entities: Entity[] | Entity, options: any = {}) {
getTotalEntityCount(entities) > USE_BACKEND_LIMIT) {

if (options.history) {
const ok = await api.confirmFn('Deleting this many entities is not undoable. Are you sure?', undefined);
const ok = await api.confirmFn('Deleting this many entities is not undoable. Are you sure?');
if (ok) {
await deleteInBackend(entities);
}
Expand Down
4 changes: 2 additions & 2 deletions src/entities/duplicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function getUniqueNameForDuplicatedEntity(entityName: string, entities: Entity[]
* @param {boolean} useUniqueName - Controls whether duplicated entity will have a unique name
* @returns {Entity} The new entity
*/
function duplicateEntity(entity: Entity, parent: Entity, ind: number, duplicatedIdsMap: Record<string, string>, useUniqueName: boolean) {
function duplicateEntity(entity: Entity, parent: Entity, ind: number, duplicatedIdsMap: Record<string, string>, useUniqueName: boolean = false) {
const originalResourceId = entity.get('resource_id');
const data = entity.json() as Record<string, any>;
const children = data.children;
Expand Down Expand Up @@ -149,7 +149,7 @@ function duplicateEntity(entity: Entity, parent: Entity, ind: number, duplicated

// add children too
children.forEach((childId: string) => {
duplicateEntity(api.entities.get(childId), entity, undefined, duplicatedIdsMap, undefined);
duplicateEntity(api.entities.get(childId), entity, undefined, duplicatedIdsMap);
});

return entity;
Expand Down
2 changes: 1 addition & 1 deletion src/entities/reparent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function reparentEntities(data: ReparentArguments[], options: any = {}) {
if (indNew !== -1 && indNew <= parent.get('children').length) {
parent.insert('children', entity.get('resource_id'), indNew);
} else {
parent.insert('children', entity.get('resource_id'), undefined);
parent.insert('children', entity.get('resource_id'));
}
parent.history.enabled = history.parent;

Expand Down
15 changes: 11 additions & 4 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,8 @@ class Entity extends Events {
* entity.insert('tags', 'a_tag');
* ```
*/
insert(path: string, value: any, index: number) {
return (this._observer as any).insert(path, value, index, undefined, undefined);
insert(path: string, value: any, index?: number) {
return this._observer.insert(path, value, index);
}

/**
Expand All @@ -619,7 +619,7 @@ class Entity extends Events {
* ```
*/
removeValue(path: string, value: any) {
return (this._observer as any).removeValue(path, value, undefined, undefined);
return this._observer.removeValue(path, value);
}

/**
Expand Down Expand Up @@ -885,7 +885,7 @@ class Entity extends Events {
let history = entity.history.enabled;
entity.history.enabled = false;
try {
(entity as any)._observer.set('parent', null, true); // silent set otherwise we run into C3 error
entity.observer.set('parent', null, true); // silent set otherwise we run into C3 error
} catch (err) {
console.error(`Error when setting parent to null for entity ${entity.get('resource_id')}`);
console.error(err);
Expand Down Expand Up @@ -995,6 +995,13 @@ class Entity extends Events {
api.entities.removeScript([this], scriptName, options);
}

/**
* The observer object for this entity.
*/
get observer() {
return this._observer;
}

/**
* The parent entity.
*
Expand Down
2 changes: 1 addition & 1 deletion src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class globals {
* @param {boolean} options.noDismiss - If true then user cannot dismiss the popup and will have to click yes or no
* @returns {Promise<boolean>} True if the user confirmed, false otherwise
*/
static confirmFn(text: string, options: { yesText?: string, noText?: boolean, noDismiss?: boolean }): Promise<boolean> {
static confirmFn(text: string, options: { yesText?: string, noText?: boolean, noDismiss?: boolean } = {}): Promise<boolean> {
return new Promise((resolve) => {
// eslint-disable-next-line no-alert
resolve(confirm(text));
Expand Down
2 changes: 1 addition & 1 deletion src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class History extends Events {
* ```
*/
addAndExecute(action: HistoryAction) {
(this._history as any).addAndExecute(action);
this._history.addAndExecute(action);
}

/**
Expand Down
Loading

0 comments on commit 9f24a9e

Please sign in to comment.