Skip to content

Commit 478ea1b

Browse files
author
Anatoly Ostrovsky
committed
Refactor storage
1 parent 25d9f04 commit 478ea1b

File tree

8 files changed

+93
-144
lines changed

8 files changed

+93
-144
lines changed

@types/core/di/ng-module/ng-module.d.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,15 @@ export class NgModule {
181181
/**
182182
* @param {string} name
183183
* @param {Function} ctor
184-
* @returns {NgModule}
185-
*/
186-
local(name: string, ctor: Function): NgModule;
187-
/**
188-
* @param {string} name
189-
* @param {Function} ctor
190-
* @returns {NgModule}
191-
*/
192-
cookie(name: string, ctor: Function): NgModule;
193-
/**
194-
* @param {string} name
195-
* @param {Function} ctor
196-
* @param {ng.StorageBackend} backendOrConfig
184+
* @param {ng.StorageType} type
185+
* @param {ng.StorageBackend} [backendOrConfig]
197186
* @returns {NgModule}
198187
*/
199188
store(
200189
name: string,
201190
ctor: Function,
202-
backendOrConfig: ng.StorageBackend,
191+
type: ng.StorageType,
192+
backendOrConfig?: ng.StorageBackend,
203193
): NgModule;
204194
/**
205195
* @template T, ID

@types/namespace.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ import {
6464
import { Provider as TProvideService } from "./interface.ts";
6565
import { Location as TLocationService } from "./services/location/location.js";
6666
import { AnimateService as TAnimateService } from "./animations/interface.ts";
67-
import { StorageBackend as TStorageBackend } from "./services/storage/interface.ts";
67+
import {
68+
StorageBackend as TStorageBackend,
69+
StorageType as TStorageType,
70+
} from "./services/storage/interface.ts";
6871
import { StreamConnectionConfig as TStreamConnectionConfig } from "./services/stream/interface.ts";
6972
import { CookieService as TCookieService } from "./services/cookie/cookie.js";
7073
import {
@@ -143,6 +146,7 @@ declare global {
143146
| (abstract new (...args: any[]) => any),
144147
> = TInjectable<T>;
145148
type StorageBackend = TStorageBackend;
149+
type StorageType = TStorageType;
146150
type StreamConnectionConfig = TStreamConnectionConfig;
147151
type CookieOptions = TCookieOptions;
148152
type CookieStoreOptions = TCookieStoreOptions;

@types/services/storage/interface.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export interface StorageBackend {
33
set(key: string, value: string): void;
44
remove(key: string): void;
55
}
6+
export type StorageType = "local" | "session" | "cookie" | "custom";

src/core/di/injector.js

Lines changed: 66 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ export function createInjector(modulesToLoad, strictDi = false) {
3636
service: supportObject(service),
3737
value: supportObject(value),
3838
constant: supportObject(constant),
39-
session: supportObject(session),
40-
local: supportObject(local),
41-
store: supportObject(store),
42-
cookie: supportObject(cookie),
39+
store,
4340
decorator,
4441
},
4542
};
@@ -174,106 +171,80 @@ export function createInjector(modulesToLoad, strictDi = false) {
174171
}
175172

176173
/**
177-
* Registers a session-persistent service
178-
*/
179-
function session(name, ctor) {
180-
return provider(name, {
181-
$get: ($injector) => {
182-
const instance = $injector.instantiate(ctor);
183-
return createPersistentProxy(instance, name, sessionStorage);
184-
},
185-
});
186-
}
187-
188-
/**
189-
* Registers a localStorage-persistent service
190-
*/
191-
function local(name, ctor) {
192-
return provider(name, {
193-
$get: ($injector) => {
194-
const instance = $injector.instantiate(ctor);
195-
return createPersistentProxy(instance, name, localStorage);
196-
},
197-
});
198-
}
199-
200-
/**
201-
* Registers a cookie-persistent service.
202-
*
203-
* @param {string} name
204-
* @param {Function} ctor
205-
* @param {ng.CookieStoreOptions} [options]
206-
*/
207-
function cookie(name, ctor, options = {}) {
208-
return provider(name, {
209-
$get: [
210-
$injectTokens.$injector,
211-
($injector) => {
212-
const instance = $injector.instantiate(ctor);
213-
const $cookie = $injector.get($injectTokens.$cookie);
214-
const serialize = options.serialize ?? JSON.stringify;
215-
const deserialize = options.deserialize ?? JSON.parse;
216-
const cookieOpts = options.cookie ?? {};
217-
218-
return createPersistentProxy(instance, name, {
219-
getItem(key) {
220-
const raw = $cookie.get(key);
221-
return raw == null ? null : raw;
222-
},
223-
224-
setItem(key, value) {
225-
$cookie.put(key, value, cookieOpts);
226-
},
227-
228-
removeItem(key) {
229-
$cookie.remove(key, cookieOpts);
230-
},
231-
232-
serialize,
233-
deserialize,
234-
});
235-
},
236-
],
237-
});
238-
}
239-
240-
/**
241-
* Registers a service persisted in a custom storage
174+
* Registers a service persisted in a storage
242175
*
243176
* @param {string} name - Service name
244177
* @param {Function} ctor - Constructor for the service
178+
* @param {ng.StorageType} type - Type of storage to be instantiated
245179
* @param {Storage|Object} backendOrConfig - Either a Storage-like object (getItem/setItem) or a config object
246180
* with { backend, serialize, deserialize }
247181
*/
248-
function store(name, ctor, backendOrConfig) {
182+
function store(name, ctor, type, backendOrConfig = {}) {
249183
return provider(name, {
250-
$get: ($injector) => {
251-
const instance = $injector.instantiate(ctor);
252-
253-
let backend;
254-
let serialize = JSON.stringify;
255-
let deserialize = JSON.parse;
256-
257-
if (backendOrConfig) {
258-
if (typeof backendOrConfig.getItem === "function") {
259-
// raw Storage object
260-
backend = backendOrConfig;
261-
} else if (isObject(backendOrConfig)) {
262-
backend = backendOrConfig.backend || localStorage;
263-
if (backendOrConfig.serialize)
264-
serialize = backendOrConfig.serialize;
265-
if (backendOrConfig.deserialize)
266-
deserialize = backendOrConfig.deserialize;
184+
$get: /** @param {ng.InjectorService} $injector */ ($injector) => {
185+
switch (type) {
186+
case "session": {
187+
const instance = $injector.instantiate(ctor);
188+
return createPersistentProxy(instance, name, sessionStorage);
189+
}
190+
case "local": {
191+
const instance = $injector.instantiate(ctor);
192+
return createPersistentProxy(instance, name, localStorage);
193+
}
194+
case "cookie": {
195+
const instance = $injector.instantiate(ctor);
196+
const $cookie = $injector.get($injectTokens.$cookie);
197+
const serialize = backendOrConfig.serialize ?? JSON.stringify;
198+
const deserialize = backendOrConfig.deserialize ?? JSON.parse;
199+
const cookieOpts = backendOrConfig.cookie ?? {};
200+
201+
return createPersistentProxy(instance, name, {
202+
getItem(key) {
203+
const raw = $cookie.get(key);
204+
return raw == null ? null : raw;
205+
},
206+
207+
setItem(key, value) {
208+
$cookie.put(key, value, cookieOpts);
209+
},
210+
211+
removeItem(key) {
212+
$cookie.remove(key, cookieOpts);
213+
},
214+
215+
serialize,
216+
deserialize,
217+
});
218+
}
219+
case "custom": {
220+
const instance = $injector.instantiate(ctor);
221+
222+
let backend;
223+
let serialize = JSON.stringify;
224+
let deserialize = JSON.parse;
225+
226+
if (backendOrConfig) {
227+
if (typeof backendOrConfig.getItem === "function") {
228+
// raw Storage object
229+
backend = backendOrConfig;
230+
} else if (isObject(backendOrConfig)) {
231+
backend = backendOrConfig.backend || localStorage;
232+
if (backendOrConfig.serialize)
233+
serialize = backendOrConfig.serialize;
234+
if (backendOrConfig.deserialize)
235+
deserialize = backendOrConfig.deserialize;
236+
}
237+
} else {
238+
// fallback default
239+
backend = localStorage;
240+
}
241+
242+
return createPersistentProxy(instance, name, backend, {
243+
serialize,
244+
deserialize,
245+
});
267246
}
268-
} else {
269-
// fallback default
270-
backend = localStorage;
271247
}
272-
273-
return createPersistentProxy(instance, name, backend, {
274-
serialize,
275-
deserialize,
276-
});
277248
},
278249
});
279250
}

src/core/di/ng-module/ng-module.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -319,43 +319,18 @@ export class NgModule {
319319
/**
320320
* @param {string} name
321321
* @param {Function} ctor
322+
* @param {ng.StorageType} type
323+
* @param {ng.StorageBackend} [backendOrConfig]
322324
* @returns {NgModule}
323325
*/
324-
local(name, ctor) {
325-
if (ctor && isFunction(ctor)) {
326-
ctor["$$moduleName"] = name;
327-
}
328-
this.invokeQueue.push([$t.$provide, "local", [name, ctor]]);
329-
return this;
330-
}
331-
332-
/**
333-
* @param {string} name
334-
* @param {Function} ctor
335-
* @returns {NgModule}
336-
*/
337-
cookie(name, ctor) {
338-
if (ctor && isFunction(ctor)) {
339-
ctor["$$moduleName"] = name;
340-
}
341-
this.invokeQueue.push([$t.$provide, "cookie", [name, ctor]]);
342-
return this;
343-
}
344-
345-
/**
346-
* @param {string} name
347-
* @param {Function} ctor
348-
* @param {ng.StorageBackend} backendOrConfig
349-
* @returns {NgModule}
350-
*/
351-
store(name, ctor, backendOrConfig) {
326+
store(name, ctor, type, backendOrConfig) {
352327
if (ctor && isFunction(ctor)) {
353328
ctor["$$moduleName"] = name;
354329
}
355330
this.invokeQueue.push([
356331
$t.$provide,
357332
"store",
358-
[name, ctor, backendOrConfig],
333+
[name, ctor, type, backendOrConfig],
359334
]);
360335
return this;
361336
}

src/namespace.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ import {
6868
import { Provider as TProvideService } from "./interface.ts";
6969
import { Location as TLocationService } from "./services/location/location.js";
7070
import { AnimateService as TAnimateService } from "./animations/interface.ts";
71-
import { StorageBackend as TStorageBackend } from "./services/storage/interface.ts";
71+
import {
72+
StorageBackend as TStorageBackend,
73+
StorageType as TStorageType,
74+
} from "./services/storage/interface.ts";
7275
import { StreamConnectionConfig as TStreamConnectionConfig } from "./services/stream/interface.ts";
7376
import { CookieService as TCookieService } from "./services/cookie/cookie.js";
7477
import {
@@ -165,6 +168,7 @@ declare global {
165168
| (abstract new (...args: any[]) => any),
166169
> = TInjectable<T>;
167170
export type StorageBackend = TStorageBackend;
171+
export type StorageType = TStorageType;
168172
export type StreamConnectionConfig = TStreamConnectionConfig;
169173
export type CookieOptions = TCookieOptions;
170174
export type CookieStoreOptions = TCookieStoreOptions;

src/services/storage/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export interface StorageBackend {
33
set(key: string, value: string): void;
44
remove(key: string): void;
55
}
6+
7+
export type StorageType = "local" | "session" | "cookie" | "custom";

src/services/storage/storage-example.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,26 @@
5454
document.addEventListener("DOMContentLoaded", () => {
5555
angular
5656
.module("app", [])
57-
.session(
57+
.store(
5858
"sessionStore",
5959
class {
6060
counter = 0;
6161
},
62+
"session",
6263
)
63-
.local(
64+
.store(
6465
"localStore",
6566
class {
6667
counter = 0;
6768
},
69+
"local",
6870
)
69-
.cookie(
71+
.store(
7072
"cookieStore",
7173
class {
7274
counter = 0;
7375
},
74-
cookieStorage,
76+
"cookie",
7577
);
7678
});
7779
</script>

0 commit comments

Comments
 (0)