-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowedEventLog.m
More file actions
197 lines (153 loc) · 5.5 KB
/
WindowedEventLog.m
File metadata and controls
197 lines (153 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// MsgCache.m
// objc-hubs-mask
//
// Created by dev on 2022-04-28.
// Copyright © 2022 Root Interface AB. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "WindowedEventLog.h"
const NSTimeInterval kti_POLL_RES_SECONDS= 1;
const NSTimeInterval knsti_ENTRY_EXP_NEVER= -1;
@interface Context : NSObject
{
NSTimeInterval _nsti_s_TTL;
NSDate* _nsdt_instant;
}
@property id entry;
@property NSDictionary* userInfo;
+ (Context*) contextWithEntry:(id)id_entry userInfo:(NSDictionary*)nsdic_userInfo andSeconds:(NSTimeInterval)nsti_s_TTL;
- (instancetype) init NS_UNAVAILABLE;
- (instancetype) initContextWithEntry:(id)id_entry userInfo:(NSDictionary*)nsdic_userInfo andSeconds:(NSTimeInterval)nsti_s_TTL;
- (bool) expired;
@end
//----------------------------------------------------------------------------------------------------------------
@implementation Context
@synthesize entry= _id_entry;
@synthesize userInfo= _id_userInfo;
+ (Context*) contextWithEntry:(id)id_entry userInfo:(NSDictionary*)nsdic_userInfo andSeconds:(NSTimeInterval)nsti_s_TTL {
return [[self alloc] initContextWithEntry:id_entry userInfo:nsdic_userInfo andSeconds:nsti_s_TTL];
}
//- (instancetype) init
//{
// self = [super init];
// if (self) {
// }
// return self;
//}
- (instancetype) initContextWithEntry:(id)id_entry userInfo:(NSDictionary*)nsdic_userInfo andSeconds:(NSTimeInterval)nsti_s_TTL
{
if ([self init]) {
_id_entry= id_entry;
_id_userInfo= nsdic_userInfo;
_nsti_s_TTL= nsti_s_TTL;
_nsdt_instant= [NSDate date];
}
return self;
}
- (bool) expired
{
if (_nsti_s_TTL == knsti_ENTRY_EXP_NEVER)
return false;
NSTimeInterval ti_since_now= [_nsdt_instant timeIntervalSinceNow]; // produces a negative time interval
if ((-ti_since_now) > _nsti_s_TTL)
return true;
return false;
}
@end
//----------------------------------------------------------------------------------------------------------------
@implementation WindowedEventLog
- (instancetype) init {
self = [self initWithSeconds:kti_POLL_RES_SECONDS];
if (self) {
}
return self;
}
- (instancetype) initWithSeconds:(NSTimeInterval)ti_poll_res_seconds
{
self = [super init];
if (self) {
_nsmarr_db= [[NSMutableArray alloc] init];
_timer= [NSTimer scheduledTimerWithTimeInterval:ti_poll_res_seconds target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
}
return self;
}
- (void) timerFireMethod:(NSTimer *)timer
{
// NSLog(@"_nsmarr_db: %@", _nsmarr_db);
NSPredicate* nspredicate= [NSPredicate predicateWithBlock:^BOOL(id evaluatedContext, NSDictionary<NSString *,id> *bindings) {
if (! [evaluatedContext isKindOfClass:[Context class]])
return NO;
return [evaluatedContext expired] ? NO : YES;
}];
[_nsmarr_db filterUsingPredicate:nspredicate];
}
- (void) cacheEntry:(id)entry {
[self cacheEntry:entry userInfo:nil andSeconds:knsti_ENTRY_EXP_NEVER];
}
- (void) cacheEntry:(id)entry andSeconds:(NSTimeInterval)nsti_s_TTL {
[self cacheEntry:entry userInfo:nil andSeconds:nsti_s_TTL];
}
- (void) cacheEntry:(id)entry userInfo:(id)userInfo {
[self cacheEntry:entry userInfo:userInfo andSeconds:knsti_ENTRY_EXP_NEVER];
}
- (void) cacheEntry:(id)entry userInfo:(id)userInfo andSeconds:(NSTimeInterval)nsti_s_TTL {
[_nsmarr_db addObject: [Context contextWithEntry:entry userInfo:userInfo andSeconds:nsti_s_TTL]];
}
- (void) enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(t_blk_Evaluate)blk_Evaluate {
[_nsmarr_db enumerateObjectsWithOptions:opts usingBlock:^(Context* context, NSUInteger nsuint_idx_evaluated, BOOL* pbool_stop) {
bool b_stop;
blk_Evaluate(nsuint_idx_evaluated, context.entry, context.userInfo, &b_stop);
*pbool_stop= (b_stop) ? YES : NO;
}];
}
- (void) enumerateUsingBlock:(t_blk_Evaluate)blk_Evaluate {
[self enumerateObjectsWithOptions:0 usingBlock:blk_Evaluate];
}
- (void) enumerateReverseUsingBlock:(t_blk_Evaluate)blk_Evaluate {
[self enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:blk_Evaluate];
}
- (void) enumerateAt:(NSUInteger)nsuint_idx usingBlock:(t_blk_Evaluate)blk_Evaluate {
[_nsmarr_db enumerateObjectsUsingBlock:^(Context* context, NSUInteger nsuint_idx_evaluated, BOOL* pbool_stop) {
if (nsuint_idx_evaluated < nsuint_idx)
return;
bool b_stop;
blk_Evaluate(nsuint_idx_evaluated, context.entry, context.userInfo, &b_stop);
*pbool_stop= (b_stop) ? YES : NO;
}];
}
- (bool) contains:(id)entry idx:(NSUInteger*)p_nsuint_idx predicateIsEqual:(t_PredicateIsEqual)predicateIsEqual
{
__block bool b_contains= false;
__block NSUInteger nsuint_idx;
[self enumerateUsingBlock:^(NSUInteger idx, id id_entry_evaluated, id id_userInfo_evaluated, bool* pb_stop) {
if (predicateIsEqual(entry, id_entry_evaluated)) {
b_contains= true;
nsuint_idx= idx;
*pb_stop= true;
}
}];
*p_nsuint_idx= nsuint_idx;
return b_contains;
}
- (bool) contains:(id)entry predicateIsEqual:(t_PredicateIsEqual)predicateIsEqual {
NSUInteger nsint_idx;
return [self contains:entry idx:&nsint_idx predicateIsEqual:predicateIsEqual];
}
- (bool) contains:(id)entry userInfo:(id*)p_userInfo predicateIsEqual:(t_PredicateIsEqual)predicateIsEqual
{
NSUInteger nsint_idx;
bool b_contains= [self contains:entry idx:&nsint_idx predicateIsEqual:predicateIsEqual];
if (! b_contains)
return b_contains;
Context* context= _nsmarr_db[nsint_idx];
*p_userInfo= context.userInfo;
return b_contains;
}
- (void) expire:(id)id_Entry {
// [_nsmdic_Cache removeObjectForKey:nsstr_Entry];
}
//- (NSEnumerator*) entryEnumerator {
// return [_nsmdic_Cache keyEnumerator];
//}
@end