Skip to content

Commit

Permalink
system level local/session "storage" event
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmtimbo committed Aug 19, 2024
1 parent 2ff4f71 commit e2ffa79
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
17 changes: 1 addition & 16 deletions src/system/platform/api/storage/LocalStorage/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ObjectUpdateType } from '../../../../../ObjectUpdateType'
import { APINotSupportedError } from '../../../../../exception/APINotImplementedError'
import { System } from '../../../../../system'
import { Dict } from '../../../../../types/Dict'
import { Unlisten } from '../../../../../types/Unlisten'
import { J } from '../../../../../types/interface/J'
import { V } from '../../../../../types/interface/V'
import { ID_LOCAL_STORAGE } from '../../../../_ids'
Expand All @@ -17,20 +15,7 @@ export default class _LocalStorage
implements J<Dict<any>>, V<Dict<string>>
{
constructor(system: System) {
super(system, ID_LOCAL_STORAGE)
}

subscribe(
path: string[],
key: string,
listener: (
type: ObjectUpdateType,
path: string[],
key: string,
data: any
) => void
): Unlisten {
throw new Error('cannot subscribe to local storage')
super(system, ID_LOCAL_STORAGE, 'local')
}

protected _storage = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/system/platform/api/storage/SessionStorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class SessionStorage
implements V, J<Dict<any>>
{
constructor(system: System) {
super(system, ID_SESSION_STORAGE)
super(system, ID_SESSION_STORAGE, 'session')
}

protected _storage = () => {
Expand Down
34 changes: 28 additions & 6 deletions src/system/platform/api/storage/Storage_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export type I = {}
export type O = {}

export default class Storage_ extends Primitive<I, O> implements V, J {
constructor(system: System, id: string) {
private _prefix: string

constructor(system: System, id: string, prefix: string) {
super(
{
i: [],
Expand All @@ -31,6 +33,8 @@ export default class Storage_ extends Primitive<I, O> implements V, J {
system,
id
)

this._prefix = prefix
}

protected _storage = (): Storage => {
Expand Down Expand Up @@ -62,19 +66,23 @@ export default class Storage_ extends Primitive<I, O> implements V, J {
}

async set(name: string, data: any): Promise<void> {
const { path } = this.__system
const { path, emitter } = this.__system

const storage = this._storage()

return set(storage, path, name, data)
set(storage, path, name, data)

emitter.emit(`${this._prefix}_storage`, name, data)
}

async delete(name: string): Promise<any> {
const { path } = this.__system
const { path, emitter } = this.__system

const storage = this._storage()

return delete_(storage, path, name)
delete_(storage, path, name)

emitter.emit(`${this._prefix}_storage`, name, undefined)
}

async deepSet(path_: string[], data: any): Promise<void> {
Expand Down Expand Up @@ -123,7 +131,21 @@ export default class Storage_ extends Primitive<I, O> implements V, J {
data: any
) => void
): Unlisten {
throw new MethodNotImplementedError()
const { emitter } = this.__system

if (path.length > 0) {
throw new ObjectPathTooDeepError()
}

return emitter.addListener(`${this._prefix}_storage`, (key_, value) => {
if (key_ === key || key === '*') {
if (value === undefined) {
listener('delete', [], key, value)
} else {
listener('set', [], key, value)
}
}
})
}

async keys(): Promise<string[]> {
Expand Down

0 comments on commit e2ffa79

Please sign in to comment.