-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from Jam3/fix/services-intellisense
Improve services intellisense
- Loading branch information
Showing
9 changed files
with
122 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Cookies from 'js-cookie'; | ||
|
||
type CookieListener = (name: string, value: string | undefined) => void; | ||
|
||
class Service { | ||
listeners: CookieListener[] = []; | ||
|
||
set = (name: string, value: string, options: Cookies.CookieAttributes = { expires: 30 }) => { | ||
Cookies.set(name, value, options); | ||
this.listeners.forEach((listener) => listener(name, value)); | ||
}; | ||
|
||
get = (name: string) => { | ||
return Cookies.get(name); | ||
}; | ||
|
||
listen = (listener: CookieListener) => { | ||
if (!this.listeners.includes(listener)) this.listeners.push(listener); | ||
}; | ||
|
||
dismiss = (listener: CookieListener) => { | ||
this.listeners = this.listeners.filter((l) => l !== listener); | ||
}; | ||
} | ||
|
||
const CookieService = new Service(); | ||
|
||
export default CookieService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
type LocalStorageListener = (name: string, value: string | undefined) => void; | ||
|
||
const KEY = 'NEXTJS-BOILERPLATE'; | ||
|
||
class Service { | ||
listeners: LocalStorageListener[] = []; | ||
|
||
set = (name: string, value: string): boolean => { | ||
try { | ||
if (value !== undefined) { | ||
const serializedData = localStorage.getItem(KEY); | ||
if (serializedData === null) { | ||
localStorage.setItem(KEY, JSON.stringify({ [name]: value })); | ||
} else { | ||
localStorage.setItem(KEY, JSON.stringify({ ...JSON.parse(serializedData), [name]: value })); | ||
} | ||
} | ||
this.listeners.forEach((listener) => listener(name, value)); | ||
return true; | ||
} catch (e) { | ||
// no local storage (ssr or incognito mode) | ||
return false; | ||
} | ||
}; | ||
|
||
get = (name: string): string | null => { | ||
try { | ||
const serializedData = localStorage.getItem(KEY); | ||
if (!serializedData) return null; | ||
return JSON.parse(serializedData)[name] || null; | ||
} catch (e) { | ||
// no local storage (ssr or incognito mode) | ||
return null; | ||
} | ||
}; | ||
|
||
listen = (listener: LocalStorageListener) => { | ||
if (!this.listeners.includes(listener)) this.listeners.push(listener); | ||
}; | ||
|
||
dismiss = (listener: LocalStorageListener) => { | ||
this.listeners = this.listeners.filter((l) => l !== listener); | ||
}; | ||
} | ||
const LocalStorageService = new Service(); | ||
|
||
export default LocalStorageService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,43 @@ | ||
import { device } from '@jam3/detect'; | ||
|
||
import { resizeDebounceTime } from '@/data/settings'; | ||
|
||
type ResizeListener = (e?: Event | UIEvent) => void; | ||
|
||
class ResizeService { | ||
let timeout: NodeJS.Timeout; | ||
class Service { | ||
listeners: ResizeListener[] = []; | ||
|
||
onResize = (e: Event | UIEvent) => { | ||
setTimeout( | ||
() => { | ||
this.listeners.forEach((listener) => listener(e)); | ||
}, | ||
device.mobile ? 500 : 0 // some mobile browsers only update window dimensions when the rotate animation finishes | ||
); | ||
clearTimeout(timeout); | ||
|
||
timeout = setTimeout(() => { | ||
this.listeners.forEach((listener) => listener(e)); | ||
if (device.mobile) { | ||
timeout = setTimeout(() => { | ||
this.listeners.forEach((listener) => listener(e)); | ||
}, 500); // some mobile browsers only update window dimensions when the rotate animation finishes | ||
} | ||
}, resizeDebounceTime); | ||
}; | ||
|
||
listen = (listener: ResizeListener) => { | ||
if (!this.listeners.length) { | ||
window.addEventListener(device.mobile ? 'orientationchange' : 'resize', this.onResize); | ||
window.addEventListener('resize', this.onResize); | ||
if (device.mobile) window.addEventListener('orientationchange', this.onResize); | ||
} | ||
if (!this.listeners.includes(listener)) this.listeners.push(listener); | ||
}; | ||
|
||
dismiss = (listener: ResizeListener) => { | ||
this.listeners = this.listeners.filter((l) => l !== listener); | ||
if (!this.listeners.length) { | ||
window.removeEventListener(device.mobile ? 'orientationchange' : 'resize', this.onResize); | ||
window.removeEventListener('resize', this.onResize); | ||
if (device.mobile) window.removeEventListener('orientationchange', this.onResize); | ||
} | ||
}; | ||
} | ||
|
||
export default new ResizeService(); | ||
const ResizeService = new Service(); | ||
|
||
export default ResizeService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters