@blocksuite/store
Advanced tools
Comparing version 0.3.0-20221221044244-a5e3701 to 0.3.0-20221221054016-12522fa
@@ -22,2 +22,3 @@ import * as Y from 'yjs'; | ||
pagesUpdated: Signal<void>; | ||
commonFieldsUpdated: Signal<void>; | ||
constructor(id: string, workspace: Workspace, awareness: Awareness); | ||
@@ -27,2 +28,8 @@ private get _yMetaRoot(); | ||
private get _yVersions(); | ||
private get _yName(); | ||
private get _yAvatar(); | ||
get name(): string; | ||
get avatar(): string; | ||
setName(val: string): void; | ||
setAvatar(val: string): void; | ||
get pageMetas(): PageMeta[]; | ||
@@ -42,2 +49,4 @@ getPageMeta(id: string): PageMeta | undefined; | ||
private _handlePageEvent; | ||
private _handleCommonFieldsEvent; | ||
private _handleEvents; | ||
} | ||
@@ -44,0 +53,0 @@ export declare class Workspace { |
@@ -15,25 +15,16 @@ import * as Y from 'yjs'; | ||
this.pagesUpdated = new Signal(); | ||
this._handlePageEvent = (_) => { | ||
const { pageMetas, _prevPages } = this; | ||
pageMetas.forEach(page => { | ||
// newly added space can't be found | ||
// unless explictly getMap after meta updated | ||
this.doc.getMap('space:' + page.id); | ||
if (!_prevPages.has(page.id)) { | ||
// Ensure following YEvent handler could be triggered in correct order. | ||
setTimeout(() => this.pageAdded.emit(page.id)); | ||
this.commonFieldsUpdated = new Signal(); | ||
this._handleEvents = (events) => { | ||
events.forEach(e => { | ||
const hasKey = (k) => e.target === this._yMetaRoot && e.changes.keys.has(k); | ||
if (e.target === this._yPages || hasKey('pages')) { | ||
this._handlePageEvent(); | ||
} | ||
}); | ||
_prevPages.forEach(prevPageId => { | ||
const isRemoved = !pageMetas.find(p => p.id === prevPageId); | ||
if (isRemoved) { | ||
this.pageRemoved.emit(prevPageId); | ||
else if (hasKey('name') || hasKey('avatar')) { | ||
this._handleCommonFieldsEvent(); | ||
} | ||
}); | ||
_prevPages.clear(); | ||
pageMetas.forEach(page => _prevPages.add(page.id)); | ||
this.pagesUpdated.emit(); | ||
}; | ||
this._workspace = workspace; | ||
this._yMetaRoot.observeDeep(this._handlePageEvent); | ||
this._yMetaRoot.observeDeep(this._handleEvents); | ||
} | ||
@@ -55,2 +46,30 @@ get _yMetaRoot() { | ||
} | ||
get _yName() { | ||
if (!this._yMetaRoot.has('name')) { | ||
return null; | ||
} | ||
return this._yMetaRoot.get('name'); | ||
} | ||
get _yAvatar() { | ||
if (!this._yMetaRoot.has('avatar')) { | ||
return null; | ||
} | ||
return this._yMetaRoot.get('avatar'); | ||
} | ||
get name() { | ||
return this._yName ? this._yName.toString() : ''; | ||
} | ||
get avatar() { | ||
return this._yAvatar ? this._yAvatar.toString() : ''; | ||
} | ||
setName(val) { | ||
this.doc.transact(() => { | ||
this._yMetaRoot.set('name', val); | ||
}); | ||
} | ||
setAvatar(val) { | ||
this.doc.transact(() => { | ||
this._yMetaRoot.set('avatar', val); | ||
}); | ||
} | ||
get pageMetas() { | ||
@@ -118,2 +137,26 @@ return this._yPages.toJSON(); | ||
} | ||
_handlePageEvent() { | ||
const { pageMetas, _prevPages } = this; | ||
pageMetas.forEach(page => { | ||
// newly added space can't be found | ||
// unless explictly getMap after meta updated | ||
this.doc.getMap('space:' + page.id); | ||
if (!_prevPages.has(page.id)) { | ||
// Ensure following YEvent handler could be triggered in correct order. | ||
setTimeout(() => this.pageAdded.emit(page.id)); | ||
} | ||
}); | ||
_prevPages.forEach(prevPageId => { | ||
const isRemoved = !pageMetas.find(p => p.id === prevPageId); | ||
if (isRemoved) { | ||
this.pageRemoved.emit(prevPageId); | ||
} | ||
}); | ||
_prevPages.clear(); | ||
pageMetas.forEach(page => _prevPages.add(page.id)); | ||
this.pagesUpdated.emit(); | ||
} | ||
_handleCommonFieldsEvent() { | ||
this.commonFieldsUpdated.emit(); | ||
} | ||
} | ||
@@ -120,0 +163,0 @@ export class Workspace { |
{ | ||
"name": "@blocksuite/store", | ||
"version": "0.3.0-20221221044244-a5e3701", | ||
"version": "0.3.0-20221221054016-12522fa", | ||
"description": "BlockSuite data store built for general purpose state management.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -217,2 +217,15 @@ /* eslint-disable @typescript-eslint/no-restricted-imports */ | ||
}); | ||
it('can set workspace common meta fields', async () => { | ||
const options = createTestOptions(); | ||
const workspace = new Workspace(options); | ||
queueMicrotask(() => workspace.meta.setName('hello')); | ||
await waitOnce(workspace.meta.commonFieldsUpdated); | ||
assert.deepEqual(workspace.meta.name, 'hello'); | ||
queueMicrotask(() => workspace.meta.setAvatar('gengar.jpg')); | ||
await waitOnce(workspace.meta.commonFieldsUpdated); | ||
assert.deepEqual(workspace.meta.avatar, 'gengar.jpg'); | ||
}); | ||
}); | ||
@@ -219,0 +232,0 @@ |
@@ -24,2 +24,3 @@ import * as Y from 'yjs'; | ||
pagesUpdated = new Signal(); | ||
commonFieldsUpdated = new Signal(); | ||
@@ -29,3 +30,3 @@ constructor(id: string, workspace: Workspace, awareness: Awareness) { | ||
this._workspace = workspace; | ||
this._yMetaRoot.observeDeep(this._handlePageEvent); | ||
this._yMetaRoot.observeDeep(this._handleEvents); | ||
} | ||
@@ -53,2 +54,36 @@ | ||
private get _yName() { | ||
if (!this._yMetaRoot.has('name')) { | ||
return null; | ||
} | ||
return this._yMetaRoot.get('name') as string; | ||
} | ||
private get _yAvatar() { | ||
if (!this._yMetaRoot.has('avatar')) { | ||
return null; | ||
} | ||
return this._yMetaRoot.get('avatar') as Y.Text; | ||
} | ||
get name() { | ||
return this._yName ? this._yName.toString() : ''; | ||
} | ||
get avatar() { | ||
return this._yAvatar ? this._yAvatar.toString() : ''; | ||
} | ||
setName(val: string) { | ||
this.doc.transact(() => { | ||
this._yMetaRoot.set('name', val); | ||
}); | ||
} | ||
setAvatar(val: string) { | ||
this.doc.transact(() => { | ||
this._yMetaRoot.set('avatar', val); | ||
}); | ||
} | ||
get pageMetas() { | ||
@@ -125,3 +160,3 @@ return this._yPages.toJSON() as PageMeta[]; | ||
private _handlePageEvent = (_: Y.YEvent<Y.Array<unknown>>[]) => { | ||
private _handlePageEvent() { | ||
const { pageMetas, _prevPages } = this; | ||
@@ -151,2 +186,21 @@ | ||
this.pagesUpdated.emit(); | ||
} | ||
private _handleCommonFieldsEvent() { | ||
this.commonFieldsUpdated.emit(); | ||
} | ||
private _handleEvents = ( | ||
events: Y.YEvent<Y.Array<unknown> | Y.Text | Y.Map<unknown>>[] | ||
) => { | ||
events.forEach(e => { | ||
const hasKey = (k: string) => | ||
e.target === this._yMetaRoot && e.changes.keys.has(k); | ||
if (e.target === this._yPages || hasKey('pages')) { | ||
this._handlePageEvent(); | ||
} else if (hasKey('name') || hasKey('avatar')) { | ||
this._handleCommonFieldsEvent(); | ||
} | ||
}); | ||
}; | ||
@@ -153,0 +207,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
382700
6317