From b0b33cc0c55dd1efc0216f65cabbd5ca5e4c6c65 Mon Sep 17 00:00:00 2001 From: Mateus Souza Date: Sun, 20 Mar 2022 21:00:34 -0300 Subject: [PATCH] Internal code switched to watch and fire --- src/extra/http.ts | 23 ++++++----------------- src/extra/route.ts | 23 ++++++----------------- 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/src/extra/http.ts b/src/extra/http.ts index 258a68d..0f391bf 100644 --- a/src/extra/http.ts +++ b/src/extra/http.ts @@ -1,3 +1,5 @@ +import { fire, watch } from "../core/observers" + declare interface HTTPRequest extends RequestInit { method: string url: string @@ -13,15 +15,12 @@ declare interface HTTPResult { declare type HTTPCallback = (details: HTTPRequest | HTTPResult) => void | Promise -let _requestBefore: Array = [] -let _requestAfter: Array = [] - /** * Add interceptor callback before each HTTP request * @param callback */ function interceptBefore(callback: HTTPCallback) { - _requestBefore.push(callback) + watch('HTTPInterceptBefore', callback) } /** @@ -29,7 +28,7 @@ function interceptBefore(callback: HTTPCallback) { * @param callback */ function interceptAfter(callback: HTTPCallback) { - _requestAfter.push(callback) + watch('HTTPInterceptAfter', callback) } /** @@ -49,17 +48,7 @@ async function request(method: string, url: string, data?: BodyInit, headers?: H headers: headers } - const runInterceptors = async (callbacks: Array, data: HTTPRequest | HTTPResult) => { - for (const callback of callbacks) { - try { - await callback.apply({}, [data]) - } catch (error) { - return Promise.reject(error) - } - } - } - - await runInterceptors(_requestBefore, request) + await fire('HTTPInterceptBefore', request) const options = Object.assign({}, request) delete options.url @@ -111,7 +100,7 @@ async function request(method: string, url: string, data?: BodyInit, headers?: H body: body } - await runInterceptors(_requestAfter, details) + await fire('HTTPInterceptAfter', details) if (!response.ok) { throw details diff --git a/src/extra/route.ts b/src/extra/route.ts index 2bdce42..298663d 100644 --- a/src/extra/route.ts +++ b/src/extra/route.ts @@ -1,4 +1,5 @@ import { on } from "../core/events" +import { fire, watch } from "../core/observers" declare interface RoutePath { path: string @@ -17,8 +18,6 @@ declare interface RouteChange { declare type RouteCallback = (change: RouteChange) => void | Promise let _routes: Array = [] -let _routeBefore: Array = [] -let _routeAfter: Array = [] let _active: RoutePath const _options = { @@ -45,7 +44,7 @@ const _options = { * @param callback */ function beforeChange(callback: RouteCallback) { - _routeBefore.push(callback) + watch('RouteChangeBefore', callback) } /** @@ -53,7 +52,7 @@ function beforeChange(callback: RouteCallback) { * @param callback */ function afterChange(callback: RouteCallback) { - _routeAfter.push(callback) + watch('RouteChangeAfter', callback) } /** @@ -270,18 +269,8 @@ function active(): RoutePath { */ async function change(toLocation: string, replace?: boolean) { - const runWatchers = async (callbacks: Array, change: RouteChange) => { - for (const callback of callbacks) { - try { - await callback.apply({}, [change]) - } catch (error) { - return Promise.reject(error) - } - } - } - const next = match(toLocation) - if( next !== null ){ + if (next !== null) { next.location = toLocation } @@ -292,7 +281,7 @@ async function change(toLocation: string, replace?: boolean) { replace: replace } - await runWatchers(_routeBefore, change) + await fire('RouteChangeBefore', change) if (change.replace) { _options.prevent = true @@ -308,7 +297,7 @@ async function change(toLocation: string, replace?: boolean) { _active = (change.next) ? change.next : null - await runWatchers(_routeAfter, change) + await fire('RouteChangeAfter', change) }